package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private EmpMapper empMapper;

    //第一个分页查询
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageResult<Clazz> selectConditionClazz(String name, LocalDate begin, LocalDate end, int page, int pageSize) {
        Page<Clazz> page1 =  new Page<>(page, pageSize);

        //分页上面的两条查询
        //sql:select * from clazz where name like '%?%' and  end_date between ? and ?
       LambdaQueryWrapper<Clazz> lqw = new LambdaQueryWrapper<>();
       lqw.eq(name.length()>0,Clazz::getName,name).eq(end!=null,Clazz::getEndDate,end);

        this.page(page1,lqw);
        List<Clazz> clazz = page1.getRecords();



        //从班级中获取老师id并用stream流转换成集合存储班主任id
        Set<Integer> set = clazz.stream().map(item -> item.getMasterId()).filter(id -> id != null).collect(Collectors.toSet());
        if(set.size() > 0 && set != null){//说明集合中有数据
            List<Emp> emps = empMapper.selectBatchIds(set);//批量查询员工数据
            //第一种方法查询班主任名称
//            for (Clazz clazz1 : clazz) {
//                for (Emp emp : emps) {
//                    if(clazz!=null &&clazz1.getMasterId()!=null  &&emp!=null &&emp.getId()!=null && clazz1.getMasterId().equals(emp.getId())){
//                        clazz1.setMasterName(emp.getName());
//                    }
//                }
//            }
            //第二种方法查询班主任名称
            Map<Integer, String> collect = emps.stream().collect(Collectors.toMap(Emp::getId, Emp::getName));
            for(Clazz clazz1 : clazz){
                clazz1.setMasterName(collect.get(clazz1.getMasterId()));
            }
        }

        //给出班级状态
        for(Clazz clazz1 : clazz){
            if(clazz1.getEndDate()!= null && clazz1.getEndDate().isBefore(LocalDate.now())){
                clazz1.setStatus("已结业");
            } else if (LocalDate.now().isBefore(clazz1.getEndDate())) {
                clazz1.setStatus("在读");
            }else{
                clazz1.setStatus("未开始");
            }
        }

        PageResult<Clazz> clazzPageResult = new PageResult<>(page1.getTotal(), clazz);
        return  clazzPageResult;
    }


    //添加班级!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveClazz(Clazz clazz) {
        /*
        * 思路:
        *  先插入班级数据 然后获取到自增的id 再根据id去员工表中查询这个id对应的员工数据 最后获取这个员工的名字
        * */
        LocalDateTime now = LocalDateTime.now();
        clazz.setCreateTime(now);
        clazz.setUpdateTime(now);
        //给班级设置班主任
        if(clazz.getMasterId()!=null){
            //根据班主任id获取班主任数据
            Emp emp = empMapper.selectById(clazz.getMasterId());
            if(emp != null){//有这个班主任数据
                //给这个班级设置班主任名称
                clazz.setMasterName(emp.getName());
                clazzMapper.insert(clazz);
            }
        }
    }


    //修改班级
    @Override
    public void updateClazzById(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.updateById(clazz);

    }



}
